package nl.project.registration;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class RegisterPeriodStarter extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String beginSub = req.getParameter("begin_subscribedate");
			String endSub = req.getParameter("end_subscribedate");
			String beginInfo = req.getParameter("begin_infodate");
			String endInfo = req.getParameter("end_infodate");
			
			if (beginSub != "" && endSub != "" && beginInfo != "") {
				if (isDate(beginSub) && isDate(endSub) && isDate(beginInfo)) {
					SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy", Locale.US);
					Calendar beginSubDate = Calendar.getInstance();
					Calendar endSubDate = Calendar.getInstance();
					Calendar beginInfoDate = Calendar.getInstance();
					Calendar endInfoDate = Calendar.getInstance();
					
					try {
						beginSubDate.setTime(sdf.parse(beginSub));
						endSubDate.setTime(sdf.parse(endSub));
						beginInfoDate.setTime(sdf.parse(beginInfo));
						endInfoDate.setTime(sdf.parse(endInfo));
					} catch (java.text.ParseException e) {
						String error = "Ingevulde data zijn niet in het juiste format.";
						System.out.println(error);
						req.setAttribute("error", error);
						req.getRequestDispatcher("/start_planner.jsp").forward(req, resp);
					}
					boolean isCorrect = false;
					if (beginSubDate.after(endSubDate)) { // Eind datum na de begin datum (inschrijving)
						if (endSubDate.before(beginInfoDate)) { // Eind datum inschrijving voor de begin datum avonden
							isCorrect = true;
						}
					}
					if (isCorrect) {
						endInfo = sdf.format(endInfoDate.getTime());
						// Storing stuff
						DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
						Key key = KeyFactory.createKey("RegisterPeriods", "RegisterPeriods");
						Entity ent = new Entity("RegisterPeriod", key);
						ent.setProperty("beginSubscribeDate", beginSub);
						ent.setProperty("endSubscribeDate", endSub);
						ent.setProperty("beginInfoDate", beginInfo);
						ent.setProperty("endInfoDate", endInfo);
						ds.put(ent);
						resp.sendRedirect("/dashboard");
						return;
					} else {
						String error = "Ingevulde periodes kloppen niet. Staan de data in de juiste volgorde?";
						System.out.println(error);
						req.setAttribute("error", error);
						req.getRequestDispatcher("/start_planner.jsp").forward(req, resp);
					}
				} else {
					String error = "Ingevulde data zijn niet in het juiste format.";
					System.out.println(error);
					req.setAttribute("error", error);
					req.getRequestDispatcher("/start_planner.jsp").forward(req, resp);
				}
			} else {
				throw new NullPointerException();
			}
		} catch (NullPointerException e) { // If above fails, one or both parameters are not set -> back to form.
			String error = "Er is iets misgegaan. Zijn allebei de velden gevuld en zijn ze juist ingevuld?";
			req.setAttribute("error", error);
			req.getRequestDispatcher("/start_planner.jsp").forward(req, resp);
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	private boolean isDate(String date) {
		boolean isDate = false;
		// checking date for dd-mm-jjjj format
		if (date.matches("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$")) {
			isDate = true;
		}
		return isDate;
	}
}